package com.craftworks.tiles;

import javax.swing.*;
import java.awt.*;
import java.io.InputStream;

public abstract class AbstractTile extends JPanel implements Tile
{
    public enum Size
    {
        LANDSCAPE(150, 100),
        WIDE_LANDSCAPE(305, 100),
        PORTRAIT(150, 205),
        WIDE_PORTRAIT(305, 205);

        public final int width, height;

        Size(int width, int height)
        {
            this.width = width;
            this.height = height;
        }
    }

    static
    {
        registerCustomFont("/fonts/SourceHanSansCN-Regular.otf");
        registerCustomFont("/fonts/InterVariable.ttf");
    }

    static void registerCustomFont(String fontFile)
    {
        try (InputStream in = AbstractTile.class.getResourceAsStream(fontFile))
        {
            if (in != null)
            {
                Font font = Font.createFont(Font.TRUETYPE_FONT, in);
                GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
            }
        } catch (Exception ex)
        {
            ex.printStackTrace(System.err);
        }
    }

    protected static final Color FOREGROUND = Color.decode("#e7e7e5");
    protected static final Color BACKGROUND = Color.decode("#292929");

    protected final Size size;

    protected AbstractTile(Size size)
    {
        this.size = size;
        initUI();
    }

    @Override
    public final JComponent getView()
    {
        return this;
    }

    private void initUI()
    {
        setLayout(null);

        setPreferredSize(new Dimension(size.width, size.height));

        setOpaque(true);
        setBackground(BACKGROUND);
        putClientProperty("FlatLaf.style", "arc:10");
    }
}
